//
//  YCFeedBackViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/24.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  学员反馈,教练评价

#import "YCFeedBackViewController.h"
#import "YCNavigationViewController.h"
#import "YCReplyViewController.h"
#import "YCPickerContainerView.h"

@interface YCFeedBackViewController ()<UITableViewDataSource,UITableViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,YCPickerContainerViewDelegate>
@property (readonly ,nonatomic) UITableView *tableView;
@property (readonly ,nonatomic) UIButton *assignScoreButton;//评分
@property (readonly ,nonatomic) YCPickerContainerView *pickerContainerView;
@property (readonly ,nonatomic) UIPickerView *scorePickerView;
@property (readonly ,nonatomic) NSArray *scoreArray;
@end

@implementation YCFeedBackViewController
@synthesize tableView = _tableView;
@synthesize assignScoreButton = _assignScoreButton;
@synthesize pickerContainerView = _pickerContainerView;
@synthesize scoreArray = _scoreArray;
@synthesize scorePickerView = _scorePickerView;

#pragma mark - 懒加载
- (YCPickerContainerView *)pickerContainerView
{
    if(!_pickerContainerView)
    {
        _pickerContainerView = [[YCPickerContainerView alloc] init];
        _pickerContainerView.delegate = self;
    }
    return _pickerContainerView;
}

- (UIPickerView *)scorePickerView
{
    if(!_scorePickerView)
    {
        _scorePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 200)];
        _scorePickerView.dataSource = self;
        _scorePickerView.delegate = self;
        _scorePickerView.backgroundColor = [UIColor whiteColor];
    }
    return _scorePickerView;
}

- (NSArray *)scoreArray
{
    if (!_scoreArray) {
        
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSInteger i=0; i <=100;i++ ) {
            NSString *scoreStr = [NSString stringWithFormat:@"%zd分",i];
            [arrayM addObject:scoreStr];
        }
        _scoreArray = arrayM;
    }
    return _scoreArray;
}

#pragma mark - 生命周期

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    [self configView];
    self.isFeedBack = NO;
    if (self.isFeedBack) {
        self.navigationItem.title = @"学员反馈";
    }else{
        self.navigationItem.title = @"教练评价";
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarResume];
}

-(void)configView
{
    CGFloat navH = 64;
    CGFloat IPHONE_HEIGHT = [UIScreen mainScreen].bounds.size.height;
    CGFloat IPHONE_WIDTH = [UIScreen mainScreen].bounds.size.width;
    
    //底层输入框高度
    CGFloat margin = 44;
    if (self.isFeedBack) {
        margin = 0;
    }
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT - margin)];
    
    //header
    UIView *view = [[UIView alloc] initWithSize:CGSizeMake(IPHONE_WIDTH, 130)];
    [view setPosition:CGPointZero atAnchorPoint:CGPointZero];
    self.tableView.tableHeaderView = view;
    
    //desc
    UILabel *descText = [[UILabel alloc] initWithSize:CGSizeMake(IPHONE_WIDTH - 25, 13)];
    [descText setPosition:CGPointMake(25, 10) atAnchorPoint:CGPointZero];
    [view addSubview:descText];
    descText.font = [UIFont systemFontOfSize:13];
    descText.textColor = [UIColor grayColor];
    descText.text = @"学员满意度(298人参与)";
    
    
    //num
    NSString *text = @"99.9";
    CGSize size = [text yc_sizeWithFont:[UIFont systemFontOfSize:38] MaxSize:CGSizeMake(MAXFLOAT, 100)];
    
    UILabel *numText = [[UILabel alloc] initWithSize:CGSizeMake(size.width, view.yc_height - CGRectGetMaxY(descText.frame))];
    [numText setPosition:CGPointMake(descText.yc_x, CGRectGetMaxY(descText.frame)) atAnchorPoint:CGPointZero];
    [view addSubview:numText];
    numText.font = [UIFont systemFontOfSize:38];
    numText.textColor = [UIColor colorWithRed:0 green:180 blue:225 alpha:1];
    numText.text = text;
    
    //person
    UILabel *p = [[UILabel alloc] initWithSize:CGSizeMake(100, numText.yc_height)];
    [p setPosition:CGPointMake(CGRectGetMaxX(numText.frame)+ 3 , CGRectGetMaxY(numText.frame) + 5) atAnchorPoint:CGPointMake(0, 1)];
    [view addSubview:p];
    p.textColor = numText.textColor;
    p.font = [UIFont systemFontOfSize:14];
    p.text = @"分";
    
    //grayLine
    UIView *grayLine = [[UIView alloc] initWithSize:CGSizeMake(IPHONE_WIDTH, 10)];
    [grayLine setPosition:CGPointMake(0,view.yc_height) atAnchorPoint:CGPointMake(0, 1)];
    [view addSubview:grayLine];
    grayLine.backgroundColor = [UIColor yc_colorWithRed:245 Green:245 Blue:245];
    
    //ischecked
    if (!self.isFeedBack) {
        
        _assignScoreButton = [[UIButton alloc] initWithSize:CGSizeMake(80, 24)];
        [_assignScoreButton setPosition:CGPointMake(kScreenWidth - 15, grayLine.yc_y - 10) atAnchorPoint:CGPointMake(1, 1)];
        [view addSubview:self.assignScoreButton];
        [_assignScoreButton setTitle:@"参与评分" forState:UIControlStateNormal];
        _assignScoreButton.layer.cornerRadius = 10;
        _assignScoreButton.layer.masksToBounds = YES;
        _assignScoreButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
        _assignScoreButton.layer.borderWidth = 1;
        _assignScoreButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_assignScoreButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_assignScoreButton addTarget:self action:@selector(assignScoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.tableView];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        
    }
    
    
}

#pragma mark - UITableView Delegate Datasource

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 50;
    }else
        return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId ;
    UITableViewCell *cell;
    
    if (indexPath.row == 0) {
        cellId = @"first";
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        
        UIView *line = [cell viewWithTag:101];
        UILabel *numLabel;
        if (!line) {
            numLabel = [[UILabel alloc] initWithSize:CGSizeMake(kScreenWidth, 30)];
            [numLabel setPosition:CGPointMake(kScreenWidth * 0.5, cell.yc_height * 0.5) atAnchorPoint:CGPointMake(0.5, 0.5)];
            numLabel.tag = 101;
            numLabel.textAlignment = NSTextAlignmentCenter;
            numLabel.font = [UIFont boldSystemFontOfSize:16];
            numLabel.textColor = [UIColor grayColor];
            [cell addSubview:numLabel];
        }
        
        numLabel.text = @"共292条评论";
        
        
        
    }else{
        cellId = @"content";
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        }
        cell.imageView.image = [UIImage yc_placeHolderImageForSize:CGSizeMake(30, 30)];
        cell.imageView.backgroundColor = [UIColor redColor];
        cell.textLabel.text  = @"小明";
        cell.detailTextLabel.text = @"熊教练认真负责";
        cell.detailTextLabel.textColor = [UIColor grayColor];
        
        
        UIView *timeView =  [cell viewWithTag:100];
        UILabel *timeLabel;
        if (!timeView) {
            timeLabel = [[UILabel alloc] initWithSize:CGSizeMake(120, 20)];
            [timeLabel setPosition:CGPointMake(kScreenWidth - 15, 10) atAnchorPoint:CGPointMake(1, 0)];
            [cell addSubview:timeLabel];
            timeLabel.textAlignment = NSTextAlignmentRight;
            timeLabel.textColor = [UIColor grayColor];
            timeLabel.font = [UIFont systemFontOfSize:13];
            [cell addSubview:timeLabel];
        }
        timeLabel.text = @"2个小时前";
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCReplyViewController *replyVC = [[YCReplyViewController alloc] init];
    [self.navigationController pushViewController:replyVC animated:NO];
}

#pragma mark - UIPickerView delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.scoreArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = self.scoreArray[row];
    return title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = self.scoreArray[row];
    YCLog(@"选中的是哪行:%@",str);
}

// pickerView点击确定时
- (void)pickerContainerViewDidDone:(YCPickerContainerView *)containerView
{
    YCLogFunc
    [self.tableView reloadData];
}


#pragma mark - 点击评分
- (void)assignScoreButtonClick:(UIButton *)button
{
    YCLogFunc
    if ([[button titleForState:UIControlStateNormal] isEqualToString:@"参与评分"]) {
        [self.pickerContainerView showPickerView:self.scorePickerView inView:self.navigationController.view];
    }
}


@end
